/**
 *@description 用户画像》营销创意偏好》线下活动
 *@author yhw
 */
import {
  activityThemeQuery,
  activityValueQuery,
  offlineAllQuery
} from '../services/MarketingOffline';
import {
  parse
} from 'qs';
import error from '../utils/tips';

export default {

  namespace: 'MarketingOffline',

  state: {
    activityThemeData: null,
    activityThemeLoading: false,
    activityValueData: null,
    activityValueLoading: false,
    offlineAllData: null,
    offlineAllLoading: false,
  },

  subscriptions: {},

  effects: { * activityThemeQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'activityThemeLoading'
      });

      const {
        data
      } = yield call(activityThemeQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'activityThemeQuerySuccess',
          payload: {
            activityThemeData: temp,
          },
        });
      } else {
        error("获取活动主题偏好失败");
      }
    },
    //===================================
    * activityValueQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'activityValueLoading'
      });

      const {
        data
      } = yield call(activityValueQuery, parse(payload));

      if (data) {
        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'activityValueQuerySuccess',
          payload: {
            activityValueData: temp,
          },
        });
      } else {
        error("获取活动利益偏好失败");
      }
    },
    //===================================
    * offlineAllQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'offlineAllLoading'
      });

      const {
        data
      } = yield call(offlineAllQuery, parse(payload));

      if (data) {
        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'offlineAllQuerySuccess',
          payload: {
            offlineAllData: temp,
          },
        });
      } else {
        error("获取线下活动整体互动意愿失败");
      }
    },

  },

  reducers: {
    activityThemeLoading(state) {
      return {...state,
        activityThemeLoading: true
      };
    },
    activityThemeQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        activityThemeLoading: false
      };
    },
    //===================================
    activityValueLoading(state) {
      return {...state,
        activityValueLoading: true
      };
    },
    activityValueQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        activityValueLoading: false
      };
    },
    //===================================
    offlineAllLoading(state) {
      return {...state,
        offlineAllLoading: true
      };
    },
    offlineAllQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        offlineAllLoading: false
      };
    },

  },
};